var salt = sessionStorage.getItem('usersalt');
var id = getQueryVariable('id');
var arr = new Array(); //图片id
var arr1 = new Array(); //文件id
var arr2 = new Array();
var danweim = '';
var dutycompay;
layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		laytpl = layui.laytpl,
		table = layui.table;

	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);

	}

	laydate.render({
		elem: '#practic',
		trigger: 'click',
		max: 0
	});

	//*获取所有企业*/
	//     riskcompay();
	//     function riskcompay(){
	//     		$.ajax({
	//			url: url+'baseEnt/getAllEnt',
	//	      		dataType: 'json',
	//	      		async: false,
	//	      		type: "POST",
	//	      		data:{
	//	      			Token:salt
	//	      		},
	//	      		success: function(data) {
	//	      			$('#dutycompay').empty();
	//	      			console.log(data);
	//	      			if(data.code==0){
	//	      				var list=data.data;
	//	      				var opt='<option value="0">请选择</option>'
	//	      				for(var i=0;i<list.length;i++){
	//	      					opt+='<option value="'+list[i].id+'" >'+list[i].name+'</option>'
	//	      				}
	//	      				$('#dutycompay1').append(opt);
	//	      				form.render();
	//	      			}
	//	
	//	      		},
	//	      		error: function(error) {
	//	      			console.log("获取失败");
	//	      		}
	//	      	});
	//     }

	/*通过隐患id获取隐患消息*/
	findinfo();

	function findinfo() {
		$.ajax({
			url: url + 'operateCard/getInfo',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
				Token: salt,
				id: id
			},
			success: function(data) {
				console.log(data);
				if(data.code == 0) {
					$('#id').html(data.data.id);
					$('#title').html(data.data.title); //操作项目
					$('#controlled').html(data.data.controlled); //受控号
					$('#releaseTime').html(data.data.releaseTime); //发布时间
					$('#reviseTime').html(data.data.reviseTime); //修订时间
					$('#staffName').html(data.data.staffName);
					$('#trainTime').html(data.data.trainTime);
					$('#trainResult').html(data.data.trainResult);
					$('#resclass').html(data.data.postName);
					if(data.data.type == 0) {
						$('#type').html("一般卡");
					} else {
						$('#type').html("关键卡");
					}
					var img = $('<div style="display:inline-block;position:relative;" id="picupfile' + flg + '"  ><a style="color: blue;text-decoration: underline;" href="' + url + 'attachment/seekFile/' +
						data.data.attId + '" >' + data.data.attName + '</a>&nbsp;<a class="layui-btn layui-btn-xs" onclick="yulan(\'' + data.data.attId + '\')">预览</a> <a class = "layui-btn layui-btn-danger layui-btn-xs" onclick="xiazai(\'' + data.data.attId + '\')"  > 下载 </div > ');
					$('#picapp').append(img);
					form.render();

				}

			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}

	/*增加治理方案*/
	$('#upsubmit').on('click', function() {

		addgovernparti();
	});

	function addgovernparti() {
		var leaderUser = $.trim($('#leaderUser1').val()); //负责人
		console.log($('#leaderUser1').val());
		//     		var dutycompay=$('#dutycompay1').val();//责任单位

		dutycompay = arr2.toString(); //责任单位

		var practic = $('#practic').val(); //落实时间
		var treatment = $.trim($('#treatment').val()); //治理方案
		console.log(leaderUser + "  " + dutycompay + " " + practic + " " + treatment);
		if(leaderUser != '' && dutycompay != 0 && practic.length != '' && treatment.length != '') {
			$.ajax({
				url: url + 'perilInfo/addBill',
				dataType: 'json',
				async: false,
				type: "POST",
				data: {
					Token: salt,
					id: idrisk,
					leaderUser: leaderUser, //负责任人、核查人
					leaderUnits: dutycompay, //负责单位、核查单位
					entTime: practic, //落实时间、核查时间
					program: treatment, //方案
					type: '0',
					attachmentsImg: arr.toString(), //图片id
					attachmentsFile: arr1.toString() //附件id
				},
				success: function(data) {
					console.log(data);
					if(data.code == 0) {
						layer.confirm('治理方案提交成功！', {
							btn: ['确定'] //按钮
						}, function() {
							location.href = "page/risk/risk-list/risk-list.html";
							layer.closeAll();
						}, function() {

						});
					}

				},
				error: function(error) {
					console.log("获取失败");
				}
			});
		} else {
			layer.msg('请将信息填写完整');
		}

	}

	var flg1 = 0;
	/*上传附件*/

	layui.use('upload', function() {
		var $ = layui.jquery,
			upload = layui.upload;

		upload.render({
			elem: '#addfile',
			url: url + 'attachment/upload',
			multiple: true,
			accept: 'file',
			before: function(obj) {},
			done: function(res) {
				//上传完毕
				console.log(res);
				if(res.code == 0) {
					flg1++;
					console.log(res);
					arr1.push(res.data.id);
					console.log(arr1);

					var img = $('<div style="display:inline-block;position:relative;margin-right:45px;" id="picupfile' + flg1 + '"  ><div onclick="delfile(\'picupfile' + flg1 + '\',\'' + flg1 + '\',\'' + res.data.id + '\')" style="position:absolute;top:-2px;right:-21px;color:red;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><a style="color: blue;text-decoration: underline;" href="' + url + 'attachment/seekFile/' + res.data.id + '" >' + res.data.originaName + '</a></div>');
					$('#picapp1').append(img);

				}
			}
		});

	});

	var flg = 0;

	/*上传照片*/

	layui.use('upload', function() {
		var $ = layui.jquery,
			upload = layui.upload;

		upload.render({
			elem: '#addpic',
			url: url + 'attachment/upload',
			multiple: true,
			accept: 'file',
			exts: 'png|jpg|gif|jpeg|PNG|JPG|GIF|JPEG',
			before: function(obj) {},
			done: function(res) {
				//上传完毕
				console.log(res);
				if(res.code == 0) {
					flg++;
					arr.push(res.data.id);

					var img = $('<div style="display:inline-block;position:relative;margin:10px;" id="picupdata' + flg + '"  ><div onclick="delpic(\'picupdata' + flg + '\',\'' + flg + '\',\'' + res.data.id + '\')" style="position:absolute;top:-4px;right:-5px;color:white;background:red;border:1px solid red;border-radius:50%;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><img id="pic' + res.data.id + '" onclick="lookbigpic(\'' + res.data.id + '\')" src="' + url + 'attachment/seekFile/' + res.data.id + '" width="100px" height="100px" class="picsty"/></div>');
					$('#picapp').append(img);

				}
			}
		});

	});

});

/*删除事件*/
function delpic(self, num, inde) {
	console.log(num);
	layer.confirm('你确定要删除吗？', {
		btn: ['确定', '取消'] //按钮
	}, function() {
		$('#' + self).remove();
		console.log(arr);
		for(var i = 0; i < arr.length; i++) {
			if(arr[i] == inde) {
				arr.splice(i, 1);
			}
		}

		console.log(arr);
		layer.msg('删除成功');
	}, function() {

	});
}

/*删除事件*/
function delfile(self, num, inde) {
	console.log(num);
	layer.confirm('你确定要删除吗？', {
		btn: ['确定', '取消'] //按钮
	}, function() {
		$('#' + self).remove();
		console.log(arr1);
		for(var i = 0; i < arr1.length; i++) {
			if(arr1[i] == inde) {
				arr1.splice(i, 1);
			}
		}

		console.log(arr1);
		layer.msg('删除成功');
	}, function() {

	});
}

function yulan(attId) {
	$.get(url + '/attachment/seekFile2/' + attId, function(data) {
		window.open(url + "file/" + data);
	});
}

function xiazai(attId) {
	window.open(url + '/attachment/seekFile/' + attId);
}

function lookbigpic(self) {

	console.log(self);
	var json = {
		"status": 1,
		"msg": "",
		"title": "",
		"id": 8,
		"start": 0,
		"data": [{
			"alt": "",
			"pid": 109,
			"src": url + "attachment/seekFile/" + self,
			"thumb": ""
		}]
	}
	layer.photos({
		photos: json,
		anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机
	});

}